package com.mq.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import utils.EmailAccount;
import utils.MailUtils;
import utils.MessageInfo;

import java.util.*;

@SpringBootApplication
@EnableAutoConfiguration
@RestController
public class MailApplication {
    private final static  String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String Sender; //读取配置文件中的参数

    @Value("${ssl.mail.username}")
    private String SSLUserName; //读取配置文件中的参数

    @Value("${ssl.mail.password}")
    private String SSLPassword; //读取配置文件中的参数
    @Value("${ssl.mail.host}")
    private String SSLHost; //读取配置文件中的参数



	public static void main(String[] args) {
		SpringApplication.run(MailApplication.class, args);

	}

    /**
     * 发送邮件 25端口发送
     * @param subject 邮件主题
     * @param text 邮件内容
     * @param to 发送的邮件地址
     * @return
     */
	@RequestMapping(value = "/sendEmail", method = RequestMethod.POST)
    public Map sendmessage(String subject,String text,String to){
        Map result = new HashMap();
        if (Objects.isNull(to)){
            result.put("message","收件人不能为空");
            return result;
        }

        if (!to.matches(REGEX_EMAIL)){
            result.put("message","收件人地址的格式不正确");
            return result;
        }
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(Sender);
        message.setTo(to);
        //邮件主题
        message.setSubject("主题:" + subject);
        //邮件的内容
        message.setText(text);
        mailSender.send(message);
        result.put("message", "邮件发送成功");
        return result;
    }
    /**
     * 发送邮件 SSL协议发送
     * @param subject 邮件主题
     * @param text 邮件内容
     * @param to 发送的邮件地址
     * @return
     */
    @RequestMapping(value = "/sendEmailSSL", method = RequestMethod.POST)
    public Map sendEmailSSL(String subject,String text,String to) throws  Exception{
        Map result = new HashMap();
        if (Objects.isNull(to)){
            result.put("message","收件人不能为空");
            return result;
        }

        if (!to.matches(REGEX_EMAIL)){
            result.put("message","收件人地址的格式不正确");
            return result;
        }
        EmailAccount account = new EmailAccount();
        account.setUsername(SSLUserName);
        account.setPassword(SSLPassword);
        account.setPlace(SSLHost);
        MessageInfo info = new MessageInfo();
        List list = new ArrayList();
        list.add(to);
        info.setTo(list);
        info.setSubject(subject);
        info.setMsg(text);
        info.setSendDate(new Date());
        MailUtils.sslSend(info,account);
        result.put("message", "邮件发送成功");
        return result;

    }
}
